﻿#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>

#include "QGeoFileTileCacheCustom.h"
#include "MapEngine.h"
#include "UrlFactory.h"

QGeoFileTileCacheCustom::QGeoFileTileCacheCustom(const QString& directory, QObject* parent) :
    QGeoFileTileCache(directory, parent)
{
}

/**
 * @brief QGeoFileTileCacheCustom::get 频繁调用
 * @param spec
 * @return
 */
QSharedPointer<QGeoTileTexture> QGeoFileTileCacheCustom::get(const QGeoTileSpec& spec)
{
    QSharedPointer<QGeoTileTexture> tt = getFromMemory(spec);
    if (tt)
        return tt;
    //    return getFromSqlite(spec);
    return getFromDisk(spec);
}

/**
 * @brief QGeoFileTileCacheCustom::insert 频繁调用
 * @param spec
 * @param bytes
 * @param format
 * @param areas
 */
void QGeoFileTileCacheCustom::insert(const QGeoTileSpec& spec, const QByteArray& bytes,
                                 const QString& format, CacheAreas areas)
{
    if (bytes.isEmpty())
        return;

    if (areas & QAbstractGeoTileCache::DiskCache) {
        //        QString filename = tileSpecToFilename(spec, format, directory_);
        //        addToSqlite(spec, format, bytes);
    }

    if (areas & QAbstractGeoTileCache::MemoryCache) {
        addToMemoryCache(spec, bytes, format);
    }

    /* inserts do not hit the texture cache -- this actually reduces overall
     * cache hit rates because many tiles come too late to be useful
     * and act as a poison */
}
